package com.house.framework.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.house.exception.MyException;
import com.house.framework.dto.UserDto;
import com.house.framework.entity.User;
import com.house.framework.query.UserQuery;
import com.house.framework.service.IUserService;
import com.house.framework.utils.AjaxResult;
import com.house.framework.utils.PageList;
import com.house.framework.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName UserController
 * @Description  用户接口
 * // @RequestMapping(value = "{id}", method = RequestMethod.GET) == @GetMapping("{id}")
 * @Version 1.0
 */
@RestController // ==@ResponseBody+@Controller
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {
    
    @Autowired
    private IUserService userService;
    
    /**
     *@Description 查询一个
     *@Param
     *@Return 
     */
    @ApiOperation(value = "用户查询一条",notes = "根据id查询")
    @GetMapping("{id}")
    public User queryById(@PathVariable("id") Long id) {
        
        return userService.findById(id);
    }
    
    /**
     *@Description 查询所有
     *@Param
     *@Return 
     */
    @ApiOperation(value = "查询所有",notes = "查询所有用户信息")
    @GetMapping
    public List<User> queryAll() {
        return userService.findAll();
    }
    
    /**
     *@Description 分页数据 页面发送请求，携带json格式
     *@Param
     *@Return
     * // @RequestBody：将请求体中json格式的参数，封装到query中
     */
    @ApiOperation(value = "分页查询",notes = "需要当前页和每页条数")
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery query) {
        return userService.queryList(query);
    }

    /**
     *@Description 删除
     *@Param
     *@Return
     */
    @ApiOperation(value = "删除操作",notes = "根据id删除")
    @GetMapping("deleteById/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try {
            userService.deleteUserById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程 高级
            return AjaxResult.me().setMsg("系统繁忙，请稍后重试...");
        }
    }

    /**
     *@Description 添加修改
     *@Param
     *@Return
     */
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user) {
        try {
            if (user.getId() == null) {
                userService.save(user);
            } else {
                userService.update(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程 高级
            return AjaxResult.me().setMsg("系统繁忙，请稍后重试...");
        }
    }

    /**
     *@Description 批量删除
     *@Param
     *@Return
     */
    @ApiOperation(value = "批量删除",notes = "根据id删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            userService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程 高级
            return AjaxResult.me().setMsg("系统繁忙，请稍后重试...");
        }
    }

    /**
     *@Description 可以使用对象接收，也可以使用map
     * dto，vo，query
     * domain，entiry，pojo... // 跟数据库表对应
     *@Param
     *@Return
     */
    @PostMapping("/phone/register")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto) {
        try {
            userService.phoneRegister(userDto);
            return AjaxResult.me();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙，稍后重试...");
        }
    }

    /**
     *@Description 资料修改
     *@Param
     *@Return
     */
    @ApiOperation(value = "用户资料修改",notes = "用户资料修改")
    @PostMapping("/dataModification")
    public void dataModification(@RequestBody User user) {
        userService.dataModification(user);
    }

    /**
     *@Description 用户列表分页
     *@Param
     *@Return
     */
    @ApiOperation(value = "用户列表分页",notes = "用户列表分页")
    @PostMapping("/userAddOrUpdate")
    public Object userAddOrUpdate(@RequestBody UserVo user) {
        if (Objects.nonNull(user.getId())) {
            return userService.userUpdate(user);
        } else {
            return userService.userAdd(user);
        }
    }


    /**
     *@Description 用户列表分页
     *@Param
     *@Return
     */
    @ApiOperation(value = "用户列表分页",notes = "用户列表分页")
    @PostMapping("/userListPaging")
    public IPage<UserVo> userListPaging(@RequestBody UserVo user) {
        return userService.userListPaging(user);
    }
}
